//
//  NetworkTool+auth.swift
//  Weibo
//
//  Created by 唐润欣 on 16/6/18.
//  Copyright © 2016年 唐润欣. All rights reserved.
//

import Foundation

extension NetworkTool {
    
    var oauthURLString: String {
        
        return "https://api.weibo.com/oauth2/authorize?client_id=\(appKey)&redirect_uri=\(redirectURL)"
    }
    
    //获取access_token
    func loadAccesstoken(code: String, callBack:([String: AnyObject]?) ->()) {
        
        let url = "https://api.weibo.com/oauth2/access_token"
        let parmeters = ["client_id": appKey, "client_secret": appSecret, "grant_type": "authorization_code", "code": code, "redirect_uri": redirectURL]
        
        //调用网络中间层成功后回调
        request(url, method: .POST, parameters: parmeters) { (response: [String: AnyObject]?) -> () in
            
            callBack(response)
        }
    }
    
    /**
     获取登录帐号信息
     
     - parameter accesstoken access_token 令牌
     - parameter uid 登录帐号的id
     - parameter callBack 获取用户帐号信息完成后回调
     */
    func loadUserAccount(accesstoken: String, uid: String, callBack: ([String: AnyObject]?) ->()) {
        
        let url = "https://api.weibo.com/2/users/show.json"
        let parameters = ["access_token": accesstoken, "uid": uid]
        
        //调用网络中间层获取用户信息
        request(url, method: .GET, parameters: parameters) { (response: [String: AnyObject]?) -> () in
            
            callBack(response)
        }
    }
}
